{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f64573a2",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "processed  10 milestones\n",
      "processed  20 milestones\n",
      "processed  30 milestones\n",
      "processed  40 milestones\n",
      "processed  50 milestones\n",
      "processed  60 milestones\n",
      "processed  70 milestones\n",
      "processed  80 milestones\n",
      "processed  90 milestones\n",
      "processed  100 milestones\n",
      "processed  110 milestones\n",
      "processed  120 milestones\n",
      "processed  130 milestones\n",
      "processed  140 milestones\n",
      "processed  150 milestones\n",
      "processed  160 milestones\n",
      "processed  170 milestones\n",
      "processed  180 milestones\n",
      "processed  190 milestones\n",
      "processed  200 milestones\n",
      "processed  210 milestones\n",
      "processed  220 milestones\n",
      "processed  230 milestones\n",
      "processed  240 milestones\n",
      "processed  250 milestones\n",
      "processed  260 milestones\n",
      "processed  270 milestones\n",
      "processed  280 milestones\n",
      "processed  290 milestones\n",
      "processed  300 milestones\n",
      "processed  310 milestones\n",
      "processed  320 milestones\n",
      "processed  330 milestones\n",
      "processed  340 milestones\n",
      "processed  350 milestones\n",
      "processed  360 milestones\n",
      "processed  370 milestones\n",
      "processed  380 milestones\n",
      "processed  390 milestones\n",
      "processed  400 milestones\n",
      "processed  410 milestones\n",
      "processed  420 milestones\n",
      "processed  430 milestones\n",
      "processed  440 milestones\n",
      "processed  450 milestones\n",
      "processed  460 milestones\n",
      "processed  470 milestones\n",
      "processed  480 milestones\n",
      "processed  490 milestones\n",
      "processed  500 milestones\n",
      "processed  510 milestones\n",
      "processed  520 milestones\n",
      "processed  530 milestones\n",
      "processed  540 milestones\n",
      "processed  550 milestones\n",
      "processed  560 milestones\n",
      "processed  570 milestones\n",
      "processed  580 milestones\n",
      "processed  590 milestones\n",
      "processed  600 milestones\n",
      "processed  610 milestones\n",
      "processed  620 milestones\n",
      "processed  630 milestones\n",
      "processed  640 milestones\n",
      "processed  650 milestones\n",
      "processed  660 milestones\n",
      "processed  670 milestones\n",
      "processed  680 milestones\n",
      "processed  690 milestones\n",
      "processed  700 milestones\n",
      "processed  710 milestones\n",
      "processed  720 milestones\n",
      "processed  730 milestones\n",
      "processed  740 milestones\n",
      "processed  750 milestones\n",
      "processed  760 milestones\n",
      "processed  770 milestones\n",
      "processed  780 milestones\n",
      "processed  790 milestones\n",
      "processed  800 milestones\n",
      "processed  810 milestones\n",
      "processed  820 milestones\n",
      "processed  830 milestones\n",
      "processed  840 milestones\n",
      "processed  850 milestones\n",
      "processed  860 milestones\n",
      "processed  870 milestones\n",
      "processed  880 milestones\n",
      "processed  890 milestones\n",
      "processed  900 milestones\n",
      "processed  910 milestones\n",
      "processed  920 milestones\n",
      "processed  930 milestones\n",
      "processed  940 milestones\n",
      "processed  950 milestones\n",
      "processed  960 milestones\n",
      "processed  970 milestones\n",
      "processed  980 milestones\n",
      "processed  990 milestones\n",
      "processed  1000 milestones\n",
      "processed  1010 milestones\n",
      "processed  1020 milestones\n",
      "processed  1030 milestones\n",
      "processed  1040 milestones\n",
      "processed  1050 milestones\n",
      "processed  1060 milestones\n",
      "processed  1070 milestones\n",
      "processed  1080 milestones\n",
      "processed  1090 milestones\n",
      "processed  1100 milestones\n",
      "processed  1110 milestones\n",
      "processed  1120 milestones\n",
      "processed  1130 milestones\n",
      "processed  1140 milestones\n",
      "processed  1150 milestones\n",
      "processed  1160 milestones\n",
      "processed  1170 milestones\n",
      "processed  1180 milestones\n",
      "processed  1190 milestones\n",
      "processed  1200 milestones\n",
      "processed  1210 milestones\n",
      "processed  1220 milestones\n",
      "processed  1230 milestones\n",
      "processed  1240 milestones\n",
      "processed  1250 milestones\n",
      "processed  1260 milestones\n",
      "processed  1270 milestones\n",
      "processed  1280 milestones\n",
      "processed  1290 milestones\n",
      "processed  1300 milestones\n",
      "processed  1310 milestones\n",
      "processed  1320 milestones\n",
      "processed  1330 milestones\n",
      "processed  1340 milestones\n",
      "processed  1350 milestones\n",
      "processed  1360 milestones\n",
      "processed  1370 milestones\n",
      "processed  1380 milestones\n",
      "processed  1390 milestones\n",
      "processed  1400 milestones\n",
      "processed  1410 milestones\n",
      "processed  1420 milestones\n",
      "processed  1430 milestones\n",
      "processed  1440 milestones\n",
      "processed  1450 milestones\n",
      "processed  1460 milestones\n",
      "processed  1470 milestones\n",
      "processed  1480 milestones\n",
      "processed  1490 milestones\n",
      "processed  1500 milestones\n",
      "processed  1510 milestones\n",
      "processed  1520 milestones\n",
      "processed  1530 milestones\n",
      "processed  1540 milestones\n",
      "processed  1550 milestones\n",
      "processed  1560 milestones\n",
      "processed  1570 milestones\n",
      "processed  1580 milestones\n",
      "processed  1590 milestones\n",
      "processed  1600 milestones\n",
      "processed  1610 milestones\n",
      "processed  1620 milestones\n",
      "processed  1630 milestones\n",
      "processed  1640 milestones\n",
      "processed  1650 milestones\n",
      "processed  1660 milestones\n",
      "processed  1670 milestones\n",
      "processed  1680 milestones\n",
      "processed  1690 milestones\n",
      "processed  1700 milestones\n",
      "processed  1710 milestones\n",
      "processed  1720 milestones\n",
      "processed  1730 milestones\n",
      "processed  1740 milestones\n",
      "processed  1750 milestones\n",
      "processed  1760 milestones\n",
      "processed  1770 milestones\n",
      "processed  1780 milestones\n",
      "processed  1790 milestones\n",
      "processed  1800 milestones\n",
      "processed  1810 milestones\n",
      "processed  1820 milestones\n",
      "processed  1830 milestones\n",
      "processed  1840 milestones\n",
      "processed  1850 milestones\n",
      "processed  1860 milestones\n",
      "processed  1870 milestones\n",
      "processed  1880 milestones\n",
      "processed  1890 milestones\n",
      "processed  1900 milestones\n",
      "processed  1910 milestones\n",
      "processed  1920 milestones\n",
      "processed  1930 milestones\n",
      "processed  1940 milestones\n",
      "processed  1950 milestones\n",
      "processed  1960 milestones\n",
      "processed  1970 milestones\n",
      "processed  1980 milestones\n",
      "processed  1990 milestones\n",
      "processed  2000 milestones\n",
      "processed  2010 milestones\n",
      "processed  2020 milestones\n",
      "processed  2030 milestones\n",
      "processed  2040 milestones\n",
      "processed  2050 milestones\n",
      "processed  2060 milestones\n",
      "processed  2070 milestones\n",
      "processed  2080 milestones\n",
      "processed  2090 milestones\n",
      "processed  2100 milestones\n",
      "processed  2110 milestones\n",
      "processed  2120 milestones\n",
      "processed  2130 milestones\n",
      "processed  2140 milestones\n",
      "processed  2150 milestones\n",
      "processed  2160 milestones\n",
      "processed  2170 milestones\n"
     ]
    }
   ],
   "source": [
    "# automated ref adding for lineage-note.txt, written by Xu Zou\n",
    "\n",
    "\n",
    "import bs4\n",
    "from bs4 import BeautifulSoup\n",
    "from urllib import request\n",
    "import os\n",
    "import json\n",
    "# Use AngieHinrich's better query code for milestones\n",
    "\n",
    "# Build milestone_list from API query that lists all milestones in a repo.\n",
    "milestone_list = []\n",
    "milestone_already=[]\n",
    "milestone_title_dict={}\n",
    "listd=os.listdir()\n",
    "\n",
    "reassign_milestone=True\n",
    "if not(\"autoref\" in listd):\n",
    "    os.mkdir(\"autoref\")\n",
    "lista=os.listdir(\"autoref\")\n",
    "\n",
    "if not(reassign_milestone):\n",
    "    if (\"milestone.txt\" in lista):\n",
    "        w=open(\"autoref/milestone.txt\",'r')\n",
    "        milestones=w.readlines()\n",
    "        for line in milestones:\n",
    "            linsp=line.split()\n",
    "            if len(linsp)>2:\n",
    "                issue=linsp[0]\n",
    "                name=linsp[1]\n",
    "                milestone_list.append([linsp[0],linsp[1],linsp[2]])\n",
    "                if linsp[1]!='withdrawn':\n",
    "                    milestone_title_dict[linsp[1]]=[linsp[2],linsp[0]]\n",
    "                milestone_already.append(issue)\n",
    "        w.close()\n",
    "\n",
    "# refresh token\n",
    "\n",
    "w=open(\"autoref/milestone.txt\",'w')\n",
    "for item in milestone_list:\n",
    "    print(item[0],item[1],item[2],file=w)\n",
    "item_count=0\n",
    "for repo in ['cov-lineages/pango-designation', 'sars-cov-2-variants/lineage-proposals']:\n",
    "    if repo == 'sars-cov-2-variants/lineage-proposals':\n",
    "        prefix = 'sars#'\n",
    "    else:\n",
    "        prefix = ''\n",
    "    # Request pages starting with 1, until we get an empty result list.\n",
    "    page = 1\n",
    "    while 1:\n",
    "        url = f'https://api.github.com/repos/{repo}/milestones?per_page=100&page={page}'\n",
    "        res_text = request.urlopen(url).read().decode('utf-8')\n",
    "        res_list = json.loads(res_text)\n",
    "        \n",
    "        if len(res_list) == 0:\n",
    "            break\n",
    "        # Each element of this page's result list includes the milestone number and title.  Parse out lineage names from title.\n",
    "        \n",
    "        for milestone_info in res_list:\n",
    "            #print(milestone_info)\n",
    "            \n",
    "            milestone = milestone_info['number']\n",
    "            milestone=prefix+str(milestone)\n",
    "            num_issues=milestone_info['open_issues']+milestone_info['closed_issues']\n",
    "            if (milestone not in milestone_already) and (num_issues>0):\n",
    "                \n",
    "                try:\n",
    "                    title = milestone_info['title']\n",
    "                    title_words = title.replace('. ','.').replace(',', ' ').split()\n",
    "                    title = title.replace(' ', '+')\n",
    "                    \n",
    "                    for item in title_words:\n",
    "                        milestone_list.append([milestone, item, title])\n",
    "                        if item!='withdrawn':\n",
    "                            # only add milestones to those without already assigned\n",
    "                            if not(item in milestone_title_dict):\n",
    "                                milestone_title_dict[item]=[title,milestone]\n",
    "                            # update sars-cov-2 to main branch\n",
    "                            if (\"sars\" in milestone_title_dict[item][1] and not('sars' in milestone)):\n",
    "                                milestone_title_dict[item]=[title,milestone]\n",
    "                        item_count+=1\n",
    "                        print(milestone, item,title,file=w)\n",
    "                        if item_count%10==0:\n",
    "                            print(\"processed \",item_count,\"milestones\")\n",
    "                except Exception as e:\n",
    "                    print(milestone, e)\n",
    "        page += 1\n",
    "w.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "88a367db",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NL.4.1+NL.4.1.1\"\n",
      "sars#2342\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "                \n",
    "def read_milestone_issues(name):\n",
    "    # or sars-cov-2 proj if it starts to add milestones\n",
    "    \n",
    "    # first, deal with multiple names\n",
    "    #print(name)\n",
    "    real_milestone=milestone_title_dict[name]\n",
    "    di='cov-lineages/pango-designation'\n",
    "    if 'sars' in real_milestone[1]:\n",
    "        di='sars-cov-2-variants/lineage-proposals'\n",
    "    actual_milestone='\"'+real_milestone[0]+'\"'\n",
    "    url=\"https://github.com/\"+di+\"/issues?q=milestone:\"+actual_milestone\n",
    "    print(url)\n",
    "    res=request.urlopen(url)\n",
    "    rp=res.read()\n",
    "    rp=rp.decode('utf-8')\n",
    "    html_soup=BeautifulSoup(rp,\"html\")\n",
    "    #print(html_soup)\n",
    "    #html=html_soup.text\n",
    "    target_attrs = {\n",
    "        'data-target': 'react-app.embeddedData',\n",
    "        'type': 'application/json'\n",
    "    }\n",
    "\n",
    "    # Find the script tag using the specified attributes\n",
    "    target_script_tag = html_soup.find('script', attrs=target_attrs)\n",
    "\n",
    "    #print(\"preloadedQueries:\",target_script_tag.string)\n",
    "    data=json.loads(target_script_tag.string)\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    try:\n",
    "        payload=data['payload']['preloadedQueries'][0]['result']['data']['repository']['search']['edges'][0]['node']\n",
    "        \n",
    "        outid=str(payload['number'])\n",
    "        if 'sars' in real_milestone[1]:\n",
    "            outid='sars#'+outid\n",
    "        return outid\n",
    "    except Exception as e:\n",
    "        print(name,e)\n",
    "        return '0'\n",
    "\n",
    "w=read_milestone_issues(\"NL.4.1.1\")\n",
    "print(w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f5b15585",
   "metadata": {},
   "outputs": [],
   "source": [
    "def examine_line(line):\n",
    "    # deal with issues/ situation\n",
    "    if 'issues/' in line:\n",
    "        linsp=line.split('issues/')\n",
    "        rel=linsp[0]+'issues/'\n",
    "        pos=0\n",
    "        while (linsp[1][pos]>='0' and linsp[1][pos]<='9' and pos<len(linsp[1])):\n",
    "            pos+=1\n",
    "        rel=rel+linsp[1][:pos]+'\\n'\n",
    "        return rel\n",
    "    if '#' in line:\n",
    "        return line\n",
    "    return ''\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a56544a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QU.1\"\n",
      "['QU.1', 'sars-cov-2-variants/lineage-proposals#2644']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NY.1+NY.1.2+NY.1.2.1+NY.3+NY.3.2+NY.4.1++NY.12+NY.14+NY.16.1+NY.17.1+NY.17.2+NY.18.1+NY.19+NY.20.1+NY.25.1+NY.26.1+NY.26.3+LP.8.1.3+LP.8.1.6+PF.1+PF.2+LP.8.1.9+NW.1+NW.1.1.1+LP.8.1.15+QE.1+PD.1+PD.2.1+PP.1+PP.1.1+PR.1+PR.2\"\n",
      "['NY.20.1', 'sars-cov-2-variants/lineage-proposals#2199']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NY.23\"\n",
      "['NY.23', 'sars-cov-2-variants/lineage-proposals#2635']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NY.1+NY.1.2+NY.1.2.1+NY.3+NY.3.2+NY.4.1++NY.12+NY.14+NY.16.1+NY.17.1+NY.17.2+NY.18.1+NY.19+NY.20.1+NY.25.1+NY.26.1+NY.26.3+LP.8.1.3+LP.8.1.6+PF.1+PF.2+LP.8.1.9+NW.1+NW.1.1.1+LP.8.1.15+QE.1+PD.1+PD.2.1+PP.1+PP.1.1+PR.1+PR.2\"\n",
      "['NY.25.1', 'sars-cov-2-variants/lineage-proposals#2199']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NY.1+NY.1.2+NY.1.2.1+NY.3+NY.3.2+NY.4.1++NY.12+NY.14+NY.16.1+NY.17.1+NY.17.2+NY.18.1+NY.19+NY.20.1+NY.25.1+NY.26.1+NY.26.3+LP.8.1.3+LP.8.1.6+PF.1+PF.2+LP.8.1.9+NW.1+NW.1.1.1+LP.8.1.15+QE.1+PD.1+PD.2.1+PP.1+PP.1.1+PR.1+PR.2\"\n",
      "['NY.26.1', 'sars-cov-2-variants/lineage-proposals#2199']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NY.1+NY.1.2+NY.1.2.1+NY.3+NY.3.2+NY.4.1++NY.12+NY.14+NY.16.1+NY.17.1+NY.17.2+NY.18.1+NY.19+NY.20.1+NY.25.1+NY.26.1+NY.26.3+LP.8.1.3+LP.8.1.6+PF.1+PF.2+LP.8.1.9+NW.1+NW.1.1.1+LP.8.1.15+QE.1+PD.1+PD.2.1+PP.1+PP.1.1+PR.1+PR.2\"\n",
      "['NY.26.3', 'sars-cov-2-variants/lineage-proposals#2199']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NW.1.2.4\"\n",
      "['NW.1.2.4', 'sars-cov-2-variants/lineage-proposals#2879']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NW.1.7+NW.1.7.2\"\n",
      "['NW.1.7.2', 'sars-cov-2-variants/lineage-proposals#2672']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NW.1.7.4+QS.1+XGB\"\n",
      "['NW.1.7.4', 'sars-cov-2-variants/lineage-proposals#2916']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NW.3.2\"\n",
      "['NW.3.2', 'sars-cov-2-variants/lineage-proposals#2664']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PD.1.2.1\"\n",
      "['PD.1.2.1', 'sars-cov-2-variants/lineage-proposals#2739']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PE.3\"\n",
      "['PE.3', 'sars-cov-2-variants/lineage-proposals#2682']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PC.3.3+PC.3.4\"\n",
      "['PC.3.3', 'sars-cov-2-variants/lineage-proposals#2659']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PC.3.3+PC.3.4\"\n",
      "['PC.3.4', 'sars-cov-2-variants/lineage-proposals#2659']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QV.1\"\n",
      "['QV.1', 'sars-cov-2-variants/lineage-proposals#2462']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QR.2\"\n",
      "['QR.2', 'sars-cov-2-variants/lineage-proposals#2759']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"MV.3\"\n",
      "['MV.3', 'sars-cov-2-variants/lineage-proposals#2306']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NB.1.1.1+QL.1\"\n",
      "['NB.1.1.1', 'sars-cov-2-variants/lineage-proposals#2789']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NB.1.1.1+QL.1\"\n",
      "['QL.1', 'sars-cov-2-variants/lineage-proposals#2789']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NB.1.5.1\"\n",
      "['NB.1.5.1', 'sars-cov-2-variants/lineage-proposals#2380']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.1.6.1\"\n",
      "['PQ.1.6.1', 'sars-cov-2-variants/lineage-proposals#2889']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.2.6\"\n",
      "['PQ.2.6', 'sars-cov-2-variants/lineage-proposals#2685']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.2.7.1\"\n",
      "['PQ.2.7.1', 'sars-cov-2-variants/lineage-proposals#2825']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.2.8.1\"\n",
      "['PQ.2.8.1', 'sars-cov-2-variants/lineage-proposals#2803']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.9.1.1\"\n",
      "['PQ.9.1.1', 'sars-cov-2-variants/lineage-proposals#2823']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.10.1.2\"\n",
      "['PQ.10.1.2', 'sars-cov-2-variants/lineage-proposals#2943']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.2.4+PQ.10.1.3+PQ.30+PQ.35\"\n",
      "['PQ.10.1.3', 'sars-cov-2-variants/lineage-proposals#2597']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.13.2\"\n",
      "['PQ.13.2', 'sars-cov-2-variants/lineage-proposals#2775']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.17.3\"\n",
      "['PQ.17.3', 'sars-cov-2-variants/lineage-proposals#2912']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.17.4\"\n",
      "['PQ.17.4', 'sars-cov-2-variants/lineage-proposals#2953']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.29\"\n",
      "['PQ.29', 'sars-cov-2-variants/lineage-proposals#2769']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.2.4+PQ.10.1.3+PQ.30+PQ.35\"\n",
      "['PQ.30', 'sars-cov-2-variants/lineage-proposals#2597']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.32\"\n",
      "['PQ.32', 'sars-cov-2-variants/lineage-proposals#2640']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.34\"\n",
      "['PQ.34', 'sars-cov-2-variants/lineage-proposals#2788']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.2.4+PQ.10.1.3+PQ.30+PQ.35\"\n",
      "['PQ.35', 'sars-cov-2-variants/lineage-proposals#2597']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"PQ.37\"\n",
      "['PQ.37', 'sars-cov-2-variants/lineage-proposals#2835']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NB.1.9\"\n",
      "['NB.1.9', 'sars-cov-2-variants/lineage-proposals#2460']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NB.1.11\"\n",
      "['NB.1.11', 'sars-cov-2-variants/lineage-proposals#2321']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NB.1.12\"\n",
      "['NB.1.12', 'sars-cov-2-variants/lineage-proposals#2575']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XDY.3+XDY.3.1\"\n",
      "['XDY.3.1', 'sars-cov-2-variants/lineage-proposals#2281']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XEC.18.1.1\"\n",
      "['XEC.18.1.1', 'sars-cov-2-variants/lineage-proposals#2690']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XEC.27.2.2\"\n",
      "['XEC.27.2.2', 'sars-cov-2-variants/lineage-proposals#2869']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.1.1\"\n",
      "['XFG.1.1', 'sars-cov-2-variants/lineage-proposals#2796']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.2.3+XFG.2.3.1\"\n",
      "['XFG.2.3', 'sars-cov-2-variants/lineage-proposals#2738']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.2.3+XFG.2.3.1\"\n",
      "['XFG.2.3.1', 'sars-cov-2-variants/lineage-proposals#2738']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.2.4\"\n",
      "['XFG.2.4', 'sars-cov-2-variants/lineage-proposals#3011']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QK.1.1\"\n",
      "['QK.1.1', 'sars-cov-2-variants/lineage-proposals#2901']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QK.2\"\n",
      "['QK.2', 'sars-cov-2-variants/lineage-proposals#2814']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QK.3\"\n",
      "['QK.3', 'sars-cov-2-variants/lineage-proposals#2724']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.1.4\"\n",
      "['XFG.3.1.4', 'sars-cov-2-variants/lineage-proposals#2857']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.1.5\"\n",
      "['XFG.3.1.5', 'sars-cov-2-variants/lineage-proposals#2866']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.1.6\"\n",
      "['XFG.3.1.6', 'sars-cov-2-variants/lineage-proposals#2728']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.12.1+XFG.3.1.7\"\n",
      "['XFG.3.1.7', 'sars-cov-2-variants/lineage-proposals#2746']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.1.10+XFG.3.14+XFG.5.2.6\"\n",
      "['XFG.3.1.10', 'sars-cov-2-variants/lineage-proposals#2768']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QT.1.1\"\n",
      "['QT.1.1', 'sars-cov-2-variants/lineage-proposals#2779']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QT.2\"\n",
      "['QT.2', 'sars-cov-2-variants/lineage-proposals#2847']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QT.3\"\n",
      "['QT.3', 'sars-cov-2-variants/lineage-proposals#2970']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"NW.1.7.4+QS.1+XGB\"\n",
      "['QS.1', 'sars-cov-2-variants/lineage-proposals#2916']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QS.2\"\n",
      "['QS.2', 'sars-cov-2-variants/lineage-proposals#2765']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.4.4\"\n",
      "['XFG.3.4.4', 'sars-cov-2-variants/lineage-proposals#2816']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.8+XFG.3.8.1\"\n",
      "['XFG.3.8', 'sars-cov-2-variants/lineage-proposals#2784']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.8+XFG.3.8.1\"\n",
      "['XFG.3.8.1', 'sars-cov-2-variants/lineage-proposals#2784']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.12.1+XFG.3.1.7\"\n",
      "['XFG.3.12.1', 'sars-cov-2-variants/lineage-proposals#2746']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.13\"\n",
      "['XFG.3.13', 'sars-cov-2-variants/lineage-proposals#2794']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.1.10+XFG.3.14+XFG.5.2.6\"\n",
      "['XFG.3.14', 'sars-cov-2-variants/lineage-proposals#2768']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.16.1\"\n",
      "['XFG.3.16.1', 'sars-cov-2-variants/lineage-proposals#2960']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.18.1\"\n",
      "['XFG.3.18.1', 'sars-cov-2-variants/lineage-proposals#2753']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.25\"\n",
      "['XFG.3.25', 'sars-cov-2-variants/lineage-proposals#2998']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.26\"\n",
      "['XFG.3.26', 'sars-cov-2-variants/lineage-proposals#2795']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.27\"\n",
      "['XFG.3.27', 'sars-cov-2-variants/lineage-proposals#2891']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.4.1.1\"\n",
      "['XFG.4.1.1', 'sars-cov-2-variants/lineage-proposals#2826']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.4.1.5\"\n",
      "['XFG.4.1.5', 'sars-cov-2-variants/lineage-proposals#2836']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.4.2\"\n",
      "['XFG.4.2', 'sars-cov-2-variants/lineage-proposals#2734']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.5.1.2\"\n",
      "['XFG.5.1.2', 'sars-cov-2-variants/lineage-proposals#2858']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.5.1.3\"\n",
      "['XFG.5.1.3', 'sars-cov-2-variants/lineage-proposals#2838']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.5.1.5\"\n",
      "['XFG.5.1.5', 'sars-cov-2-variants/lineage-proposals#2861']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.5.2.1\"\n",
      "['XFG.5.2.1', 'sars-cov-2-variants/lineage-proposals#2808']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.3.1.10+XFG.3.14+XFG.5.2.6\"\n",
      "['XFG.5.2.6', 'sars-cov-2-variants/lineage-proposals#2768']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QW.1\"\n",
      "['QW.1', 'sars-cov-2-variants/lineage-proposals#2896']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.6.1\"\n",
      "['XFG.6.1', 'sars-cov-2-variants/lineage-proposals#2829']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.6.2.1\"\n",
      "['XFG.6.2.1', 'sars-cov-2-variants/lineage-proposals#2962']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.6.6\"\n",
      "['XFG.6.6', 'sars-cov-2-variants/lineage-proposals#2987']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.7.1.1\"\n",
      "['XFG.7.1.1', 'sars-cov-2-variants/lineage-proposals#2906']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.9.2\"\n",
      "['XFG.9.2', 'sars-cov-2-variants/lineage-proposals#2910']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.9.4.1\"\n",
      "['XFG.9.4.1', 'sars-cov-2-variants/lineage-proposals#2937']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.9.5\"\n",
      "['XFG.9.5', 'sars-cov-2-variants/lineage-proposals#2830']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.10.4\"\n",
      "['XFG.10.4', 'sars-cov-2-variants/lineage-proposals#2936']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.14.2.1\"\n",
      "['XFG.14.2.1', 'sars-cov-2-variants/lineage-proposals#2954']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.14+XFG.14.3\"\n",
      "['XFG.14.3', 'sars-cov-2-variants/lineage-proposals#2624']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.17.1\"\n",
      "['XFG.17.1', 'sars-cov-2-variants/lineage-proposals#2701']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.17.2+XFG.17.2.1\"\n",
      "['XFG.17.2', 'sars-cov-2-variants/lineage-proposals#2828']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.17.2+XFG.17.2.1\"\n",
      "['XFG.17.2.1', 'sars-cov-2-variants/lineage-proposals#2828']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"QQ.1\"\n",
      "['QQ.1', 'sars-cov-2-variants/lineage-proposals#2988']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.17.3+XFG.18.1+XFG.23.1\"\n",
      "['XFG.17.3', 'sars-cov-2-variants/lineage-proposals#2586']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.17.4\"\n",
      "['XFG.17.4', 'sars-cov-2-variants/lineage-proposals#2867']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.22.1\"\n",
      "['XFG.22.1', 'sars-cov-2-variants/lineage-proposals#2881']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.24\"\n",
      "['XFG.24', 'sars-cov-2-variants/lineage-proposals#2676']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.26\"\n",
      "['XFG.26', 'sars-cov-2-variants/lineage-proposals#2718']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.32\"\n",
      "['XFG.32', 'sars-cov-2-variants/lineage-proposals#2744']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFG.34\"\n",
      "['XFG.34', 'sars-cov-2-variants/lineage-proposals#2942']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFJ.1.2\"\n",
      "['XFJ.1.2', 'sars-cov-2-variants/lineage-proposals#2622']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFJ.2.1\"\n",
      "['XFJ.2.1', 'sars-cov-2-variants/lineage-proposals#2599']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFJ.3+XFJ.3.1\"\n",
      "['XFJ.3.1', 'sars-cov-2-variants/lineage-proposals#2604']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFV.1\"\n",
      "['XFV.1', 'sars-cov-2-variants/lineage-proposals#2841']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFV.2\"\n",
      "['XFV.2', 'sars-cov-2-variants/lineage-proposals#2950']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFW+XFW.1+XFW.1.1\"\n",
      "['XFW.1', 'sars-cov-2-variants/lineage-proposals#2740']\n",
      "https://github.com/sars-cov-2-variants/lineage-proposals/issues?q=milestone:\"XFW+XFW.1+XFW.1.1\"\n",
      "['XFW.1.1', 'sars-cov-2-variants/lineage-proposals#2740']\n"
     ]
    }
   ],
   "source": [
    "# read lineage_notes.txt\n",
    "\n",
    "\n",
    "def milestone2issue(num):\n",
    "    return 0\n",
    "w=open(\"lineage_notes.txt\",'r',encoding='utf-8')\n",
    "lineage_notes=w.readlines()\n",
    "w.close()\n",
    "milestone_tobecheck=[]\n",
    "\n",
    "\n",
    "changed_items=0\n",
    "missing_milestones=[]\n",
    "for num in range(1,len(lineage_notes)):\n",
    "    line=lineage_notes[num]\n",
    "    linsp=line.split()\n",
    "    if (len(linsp)>=2):\n",
    "        title=linsp[0]\n",
    "        # for all lineages without an issue label but with a milestone\n",
    "        if not('#' in line or 'issues/' in line):\n",
    "            if title in milestone_title_dict:\n",
    "                # this time we need to deal with the milestone \n",
    "                #print(title)\n",
    "                issue_num=read_milestone_issues(title)\n",
    "                if issue_num!='0':\n",
    "                    if 'sars' in issue_num:\n",
    "                        issue_num=issue_num.replace('sars','sars-cov-2-variants/lineage-proposals')\n",
    "                    else:\n",
    "                        issue_num='#'+issue_num\n",
    "                        \n",
    "                    missing_milestones.append([title,issue_num])\n",
    "                    print(missing_milestones[-1])\n",
    "        if ('lineage-proposals#' in line):\n",
    "            if title in milestone_title_dict:\n",
    "                if not('sars' in milestone_title_dict[title][1]):\n",
    "                    issue_num=read_milestone_issues(title)\n",
    "                    if issue_num!='0':\n",
    "                        issue_num='#'+issue_num\n",
    "\n",
    "                        missing_milestones.append([title,issue_num])\n",
    "                        print(missing_milestones[-1])\n",
    "                    \n",
    "#rewrite lineage_notes.txt\n",
    "w=open(\"autoref/missing_refs.txt\",'w')\n",
    "for item in missing_milestones:\n",
    "    print(item[0],item[1],file=w)\n",
    "w.close()\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1c9369e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Add  0  references from cov-lineages/pango-designation.\n",
      "Upgrade  0  references from cov-lineages/pango-designation.\n",
      "Add  107  references from sars-cov-2-variants/lineage-proposals.\n"
     ]
    }
   ],
   "source": [
    "#final rewrite of lineage_notes.txt\n",
    "w=open(\"autoref/missing_refs.txt\",'r',encoding='utf-8')\n",
    "data=w.readlines()\n",
    "w.close()\n",
    "missing_refs={}\n",
    "for line in data:\n",
    "    linsp=line.split()\n",
    "    missing_refs[linsp[0]]=linsp[1].replace('\\n','')\n",
    "\n",
    "w=open(\"lineage_notes.txt\",'r',encoding='utf-8')\n",
    "data=w.readlines()\n",
    "w.close()\n",
    "normal_count=0\n",
    "sub_count=0\n",
    "upgrade_count=0\n",
    "for i in range(len(data)):\n",
    "    linsp=data[i].split()\n",
    "    \n",
    "    if (len(linsp)>0) and (linsp[0] in missing_refs):\n",
    "        if \"，from\" in data[i]:\n",
    "            data[i]=data[i].split(\", from\")[0]\n",
    "            upgrade_count+=1\n",
    "        data[i]=data[i].replace('\\n','')+\", from \"+missing_refs[linsp[0]]+'\\n'\n",
    "        if 'sars' in missing_refs[linsp[0]]:\n",
    "            sub_count+=1\n",
    "        else:\n",
    "            normal_count+=1\n",
    "\n",
    "print(\"Add \",normal_count-upgrade_count,\" references from cov-lineages/pango-designation.\" )\n",
    "print(\"Upgrade \",upgrade_count,\" references from cov-lineages/pango-designation.\" )\n",
    "print(\"Add \",sub_count,\" references from sars-cov-2-variants/lineage-proposals.\")\n",
    "\n",
    "w=open(\"lineage_notes.txt\",'w',newline='\\n',encoding='utf-8')\n",
    "w.writelines(data)\n",
    "w.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7756372f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# replace all \"From\" with \", from\" \n",
    "w=open(\"lineage_notes.txt\",'r')\n",
    "lines=w.readlines()\n",
    "for i in range(len(lines)):\n",
    "    lines[i]=lines[i].replace(\" From\",\", from\")\n",
    "w.close()\n",
    "w=open(\"lineage_notes.txt\",'w',newline='\\n')\n",
    "w.writelines(lines)\n",
    "w.close()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2e7c50c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
